﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using LY.Common;
using LY.DataAccess.Repository;
using LY.Entity;
using LY.Interface;
using LY.Entity.Entities;
using LY.Entity.PageSearch;
using LY.Entity.SearchEntity;

namespace LY.Business.ServiceImpl
{
    public class DictService : IDictService
    {


        private DictRepository _repo;

        public DictService()
        {
            _repo = new DictRepository();
        }

        #region 字典

        /// <summary>
        /// 字典目录列表
        /// </summary>
        /// <returns></returns>
        public List<S_DictDir> GetDictDirList()
        {
            return _repo.GetDictDirList();
        }

        /// <summary>
        /// 可分页字典目录列表
        /// </summary>
        /// <param name="search"></param>
        /// <param name="view"></param>
        /// <returns></returns>
        public PagedList<S_DictDir> QueryDictDirList(DictDirSearch search, PageView view)
        {
            return _repo.QueryDictDirList(search, view);
        }
        
        /// <summary>
        /// 字典目录实体
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public S_DictDir GetDictDir(int? id)
        {
            return _repo.GetDictDir(id);
        }

        /// <summary>
        /// 保存字典目录
        /// </summary>
        /// <param name="dictdir"></param>
        /// <returns></returns>
        public int SaveDictDir(S_DictDir dictdir)
        {

            if (dictdir.ID <= 0)
            {
                //新增
                int ret = _repo.CheckDictDir(dictdir.Name);
                if (ret > 0)
                {
                    return -2; //已存在相同的字典目录
                }
                return (int) _repo.Insert(dictdir);
            }
            else
            {
                //编辑
                int ret = _repo.CheckDictDir(dictdir.Name);
                if (ret >= 2 || ret == 1)
                {

                }
                return (int) _repo.Update(dictdir);
            }
        }

        /// <summary>
        /// 删除字典目录
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public int DeleteDictDir(int? id)
        {
            return _repo.DeleteDictDir(id);
        }

        #endregion

        #region 字典项

        /// <summary>
        /// 字典项列表
        /// </summary>
        /// <returns></returns>
        public List<S_DictItem> GetDictItemList()
        {
            return _repo.GetDictItemList();
        }


        /// <summary>
        /// 可分页字典项列表
        /// </summary>
        /// <param name="search"></param>
        /// <param name="view"></param>
        /// <returns></returns>
        public PagedList<S_DictItem> QueryDictItemList(DictItemSearch search, PageView view)
        {
            return _repo.QueryDictItemList(search, view);
        }

        /// <summary>
        /// 字典项实体
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public S_DictItem GetDictItem(int? id)
        {
            return _repo.GetDictItem(id);
        }

        /// <summary>
        /// 保存字典项
        /// </summary>
        /// <param name="dictitem"></param>
        /// <returns></returns>
        public int SaveDictItem(S_DictItem dictitem)
        {

            if (dictitem.ID <= 0)
            {
                //新增
                int ret = _repo.CheckDictItem(dictitem.Name);
                if (ret > 0)
                {
                    return -2; //已存在相同的字典项名
                }
                return (int) _repo.Insert(dictitem);
            }
            else
            {
                //编辑
                int ret = _repo.CheckDictItem(dictitem.Name);
                if (ret >= 2 || ret == 1)
                {

                }
                return (int) _repo.Update(dictitem);
            }
        }

        //删除用户
        /// <summary>
        /// 删除字典项
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public int DeleteDictItem(int? id)
        {
            return _repo.DeleteDictItem(id);
        }

        #endregion

    }
}
